<template>
  <el-dialog width="600px" :title="dialogTitle" :visible.sync="dialogVisible" @close="handleClose">
    <el-form
      ref="submitForm"
      v-loading="formLoading"
      :model="submitForm"
      :rules="submitFormRules"
      label-width="80px"
      label-position="right"
      class="submit-form"
    >
      <el-form-item label="名称" prop="name">
        <el-input v-model="submitForm.name" placeholder="请输入材料名称" />
      </el-form-item>
      <el-form-item label="含税单价" prop="unitPrice">
        <el-input-number
          v-model="submitForm.unitPrice"
          :precision="6"
          :step="0.000001"
          :max="9999999999"
          style="width: 100%"
          placeholder="请输入含税单价"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <dict-select v-model="submitForm.unit" dict-key="bom_unit" placeholder="请选择单位" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="submitForm.remark" type="textarea" placeholder="请输入备注" :autosize="{ minRows: 2 }" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="formLoading" type="text" @click="handleClose">取消</el-button>
      <el-button :loading="formLoading" type="primary" @click="handleSubmit">提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { saveMaterial, updateMaterialById, selectMaterialById } from '@/api/erp/material'
/**
 * @author yizuomin
 * @date 2023/5/7 19:05
 **/
export default {
  name: 'SubmitModule',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: this.visible,
      dialogTitle: '',
      formLoading: false,
      submitForm: {
        id: '',
        name: '',
        unit: '',
        unitPrice: '',
        remark: ''
      },
      submitFormRules: {
        name: [{ required: true, message: '请输入材料名称', trigger: 'change' }],
        unit: [{ required: true, message: '请选择单位', trigger: 'change' }],
        unitPrice: [{ required: true, message: '请输入含税单价', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(id) {
      if (!id) {
        this.dialogTitle = '新增材料'
        return
      }
      this.dialogTitle = '修改材料'
      this.formLoading = true
      selectMaterialById(id).then(resp => {
        if (resp.success) {
          this.submitForm = resp.data
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    handleSubmit() {
      this.$refs.submitForm.validate((valid) => {
        if (valid) {
          let request = saveMaterial
          if (this.submitForm.id) {
            request = updateMaterialById
          }
          this.formLoading = true
          request(this.submitForm).then(resp => {
            if (resp.success) {
              this.$message.success('操作成功')
              this.$refs.submitForm.resetFields()
              this.handleClose()
            }
          }).finally(() => {
            this.formLoading = false
          })
        }
      })
    },
    handleClose() {
      this.$emit('close')
    }
  }
}
</script>

<style scoped>

</style>
